package com.wx.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wx.dao.UserDao;
import com.wx.model.Users;
import com.wx.service.UserService;
import com.wx.serviceImpl.UserServiceImpl;
import com.wx.util.DBUtil;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/servlet/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password1");
		UserService userservice=new UserServiceImpl();
		Users user=new Users();
		user.setUsername(username);
		user.setPassword(password);
		System.out.println(username);
		try {
			boolean checkUsername = userservice.checkUsername(username);
			System.out.println(checkUsername);
			if(!checkUsername){
				boolean b = userservice.regist(user);
				if(b){
					System.out.println("zhucechenggong");
					response.sendRedirect("../login.jsp");
				}else{
					System.out.println("zhuceshibai");
					response.sendRedirect("../regist.jsp");
				}
			}else{
				response.sendRedirect("../fail.jsp");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	//	doGet(request, response);
	}
}
